package com.kkb.controller.jbinfocontroller;

import com.kkb.bean.JbShopinfo;
import com.kkb.bean.JbTags;
import com.kkb.service.jbinfoservice.JbInfoService;
import com.kkb.service.jbinfoservice.jbinfoserviceiml.JbInfoServiceIml;
import com.kkb.service.shopinfotagsservice.ShopInfoTagsService;
import com.kkb.service.shopinfotagsservice.shopinfotagsserviceiml.ShopInfoTagsServiceIml;
import com.kkb.vo.BaseResp;
import com.kkb.vo.jbInfor.req.JbInfoReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("jbShopInfo")
public class JbShopInfoController {
    @Autowired
    private JbInfoService jbInfoService;
    @Autowired
    private JbInfoServiceIml jbShopinfo;
    @Autowired
    private ShopInfoTagsServiceIml shopInfoTags;
    @Autowired
   private JbInfoReq jbInfoReq;
    /**
     * 根据id查询店铺所有信息用于回显
     * @param id
     * @return list
     *
     */
    @ResponseBody
    @RequestMapping(value = "selectById",method = RequestMethod.POST)
    public BaseResp<Map<String, Object>> selectById(Integer id,HttpServletRequest request){
       JbShopinfo jbShopinfo=jbInfoService.selectById(id);

            jbShopinfo.setsLogo(request.getServletContext().getRealPath("/upload/img")+"/"+jbShopinfo.getsLogo());
            jbShopinfo.setsToutu(request.getServletContext().getRealPath("/upload/img")+"/"+jbShopinfo.getsToutu());
        Map<String,Object> map=new HashMap<>();
       System.out.println(jbShopinfo);
        map.put("jbShopinfo",jbShopinfo);
        return new BaseResp<>(map);
    }
    /**
     * 根据id修改店铺信息
     * @param jbInfoReq
     * @return result
     */
    @ResponseBody
    @RequestMapping(value = "updateById",method = RequestMethod.POST)
    public BaseResp<Map<String,Object>> updateById(HttpServletRequest request, JbInfoReq jbInfoReq) {
        System.out.println(request);
        System.out.println(jbInfoReq);
        boolean flag = upload(request);
        if (flag) {

            try {
                System.out.println(jbInfoReq.getsLogo());
                System.out.println(jbInfoReq.getsToutu());
                jbInfoService.updateById(jbInfoReq);
                Integer id = jbInfoReq.getsId();
                shopInfoTags.deleteJbMidById(id);
                shopInfoTags.insertjbMidByIds(jbInfoReq);
            } catch (Exception e) {
                e.printStackTrace();
                return new BaseResp<>("1000", false, "修改失败");

            }
            Map<String, Object> map = new HashMap<>();
            map.put("flag", flag);
            return new BaseResp<>(map);
        }
        return new BaseResp<>("1000", false, "格式不对");
    }
    public boolean upload(HttpServletRequest request) {

        List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                .getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
         String url;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    String originalFilename=  file.getOriginalFilename();
                    String s= originalFilename.substring(originalFilename.lastIndexOf("."));
                    if(!s.toLowerCase().equals(".png") && !s.toLowerCase().equals(".jpg")&& !s.toLowerCase().equals(".jpeg")&& !s.toLowerCase().equals(".bmp")){
                        return false;
                    }
                    String realPath = request.getServletContext().getRealPath("/upload/img")+"/";
                    String fileName= UUID.randomUUID().toString().replace("-","") + originalFilename.substring(originalFilename.lastIndexOf("."));
                    url=realPath+fileName;
                    System.out.println(url);
                    File file1=new File(url);
                    stream = new BufferedOutputStream(new FileOutputStream(file1));
                    stream.write(bytes);
                    stream.close();
                    if(i==0){

                        jbInfoReq.setsLogo(fileName);
                        System.out.println(jbInfoReq.getsLogo());
                    }else {
                        jbInfoReq.setsToutu(fileName);
                        System.out.println(jbInfoReq.getsToutu());

                    }
                } catch (Exception e) {
                    stream = null;
                    return false;
                }

            } else {
                return false;
            }
        }
        return true ;
    }

    /**
     * 查询所有标签
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectTags",method = RequestMethod.POST)
    public BaseResp<Map<String,Object>> selectTags(){
        List<JbTags> list=jbShopinfo.selectTags();
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        return new BaseResp<>(map);
    }

}






